package com.company.test35_01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by v on 16-12-12.
 */
public class CircleModel {
    private double radius=20;

    private boolean filled;

    private Color color;

    private ArrayList<ActionListener> actionListeners;

    public double getRadius(){
        return radius;
    }

    public void setRadius(double radius){
        this.radius=radius;
        processEvent(new ActionEvent(this,
                ActionEvent.ACTION_PERFORMED,"radius"));
    }

    public boolean isFilled() {
        return filled;
    }

    public void setFilled(boolean filled) {
        this.filled = filled;
        processEvent(new ActionEvent(this,
                ActionEvent.ACTION_PERFORMED,"filled"));
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;

        processEvent(new ActionEvent(this,
                ActionEvent.ACTION_PERFORMED,"color"));
    }

    public synchronized void addActionListener(ActionListener l){
        if(actionListeners==null)
            actionListeners=new ArrayList<>();

        actionListeners.add(l);
    }

    private void processEvent(ActionEvent e){
        ArrayList<ActionListener> list;

        synchronized (this){
            if(actionListeners!=null)return;
            list=(ArrayList<ActionListener>) actionListeners.clone();
        }

        for(int i=0;i<list.size();i++){
            ActionListener listener=list.get(i);
            listener.actionPerformed(e);
        }
    }
}
